﻿Ext.define('app.system.substance.Edit',
{
    extend: 'Ext.window.Window',
    autoScroll: true,
    modal: true,
    maximized: true,
    layout: 'fit',

    cdx: '',
    img: '',
    oldImg: '',
    mol: '',

    draftId: 0,
    subId: 0,
    initComponent: function () {
        var me = this;
        me.items = [me.getForm()];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                scrollable: 'y',
                items: [
                {
                    xtype: 'container',
                    layout: 'column',

                    items: [
                        {
                            xtype: 'container',
                            columnWidth: 0.5,
                            items: [
                                this.getMolContainer()
                            ]

                        },
                        {
                            xtype: 'container',
                            columnWidth: 0.5,
                            items: [
                                this.getFormRightContainer()
                            ]

                        }
                    ]

                }
                ]
            });
        }
        return this.form;
    },
    getMolContainer: function () {
        if (!this.molContainer) {
            var me = this;
            this.molContainer = new Ext.panel.Panel({
                height: 290,
                tbar: [
                "->",
                {
                    text: 'Paste',
                    icon: '/Content/images/paste_plain.png',
                    handler: function (e) {
                        _____CallSocketCompentType = 'chemdraw';
                        _____CallSocketCompentId = me.getId();
                        _____ChemDrawSocket.send('GetImg');
                        _____ChemDrawSocket.send('GetCdx');
                        _____ChemDrawSocket.send('GetMol');
                    }
                },
                {
                    text: 'Edit',
                    icon: '/Content/images/edit.gif',
                    handler: function (e) {
                        var ______cdx = 'VmpDRDAxMDAEAwIBAAAAAAAAAAAAAACAAAAAAAMADwAAAENoZW1EcmF3IDEyLjAIAA4AAAB1bnRpdGxlZC5jZHgEAhAAAAAAAAAAAAAAAAAAAAAAAAEJCAAAAAAAAAAAAAIJCAAAQOUBAECcAgAJAAANCAEAAQgHAQABOgQBAAE7BAEAAEUEAQABPAQBAAAMBgEAAQ8GAQABDQYBAABCBAEAAEMEAQAARAQBAAAKCAgAAwBgAMgAAwALCAgAAwAAAMgAAwAJCAQAAIACAAgIBACZmQEABwgEAJmZAAAGCAQAAAACAAUIBABmZg4ABAgCALQAAwgEAAAAeAAjCAEABQwIAQAAKAgBAAEpCAEAASoIAQABAggQAAAAJAAAACQAAAAkAAAAJAABAwIAAAACAwIAAQAAAzIACAD///////8AAAAAAAD//wAAAAD/////AAAAAP//AAAAAP////8AAAAA/////wAA//8AAQ8AAAABAAMA5AQFAEFyaWFsAAh4AAADAAABIAEgAAAAAAtmCKD/hP+IC+MJGANnBScD/AACAAABIAEgAAAAAAtmCKAAAQBkAGQAAAABAAEBAQAAAAEnDwABAAEAAAAAAAAAAAAAAAAAAgAZAZAAAAAAAGAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAGABgAAAAQCEAAAAAAAAAAAAADAzwIAABwCFggEAAAAJAAYCAQAAAAkABkIAAAQCAIAAQAPCAIAAQAAAAAAAAA=';
                        if (me.cdx && me.cdx.length > 10) {
                            ______cdx = me.cdx;
                        }
                        _____ChemDrawSocket.send('OpenCdx:' + ______cdx);
                    }
                }
                ,
                {
                    text: 'Clear',
                    icon: '/Content/images/delete.gif',
                    handler: function (e) {
                        me.cdx = '';
                        me.img = '';
                        me.mol = '';
                        me.getMolContainer().update('___*___*___*___');
                    }
                }
                ],
                html: "___*___*___*___"
            });
        }
        return this.molContainer;
    },
    getFormRightContainer: function () {
        if (!this.formRightContainer) {
            this.formRightContainer = {
                xtype: 'panel', layout: 'form', border: false,
                defaults: { anchor: '100%', xtype: 'textfield', labelWidth: 200, labelAlign: "right" },
                items: [
                    {
                        xtype: 'hidden',
                        name: 'Id',
                        value: 0
                    },
                     {
                         fieldLabel: '中文名',
                         allowBlank: false,
                         name: 'CnName'
                     },
                    {
                        xtype: 'textarea',
                        height: 55,
                        fieldLabel: '英文名',
                        name: 'EnName'
                    },
                    {
                        fieldLabel: '分子式',
                        name: 'Formula'
                    },
                     {
                         fieldLabel: '分子量',
                         name: 'MolecularWeight'
                     },
                     {
                         fieldLabel: 'CAS',
                         name: 'CAS'
                     },
                    {
                        xtype: 'textarea',
                        height: 55,
                        fieldLabel: '高风险',
                        name: 'HighRisk'
                    },
                    {
                        xtype: 'textarea',
                        height: 55,
                        fieldLabel: '备注',
                        name: 'Note'
                    }
                ]
            };
        }
        return this.formRightContainer;
    },
    showImg: function () {
        var me = this;
        if (!me.img || me.img.length < 10) {
            return;
        }

        var image = new Image();
        image.src = 'data:image/png;base64,' + me.img;
        var this_imgWidth = 0;
        var this_imgHeight = 0;


        var iwidth = this.getMolContainer().getWidth();
        var iheight = this.getMolContainer().getHeight() - 20;

        if (image.width > 0 && image.height > 0) {
            if (image.width / image.height >= iwidth / iheight) {
                if (image.width > iwidth) {
                    this_imgWidth = iwidth;
                    this_imgHeight = (image.height * iwidth) / image.width;
                }
                else {
                    this_imgWidth = image.width;
                    this_imgHeight = image.height;
                }
            }
            else {
                if (image.height > iheight) {
                    this_imgHeight = iheight;
                    this_imgWidth = (image.width * iheight) / image.height;
                }
                else {
                    this_imgWidth = image.width;
                    this_imgHeight = image.height;
                }
            }
            if (this_imgHeight < 30) {
                this_imgHeight = 30;
            }
        }
        var xtpl = new Ext.XTemplate("<table border='0' width='100%'><tr><td  align='center' valign='middle'><img src='data:image/png;base64,{imgData}' height='{height}' width='{width}' border='{border}'/></td></tr></table>");
        var html = xtpl.applyTemplate(
            {
                imgData: me.img,
                height: this_imgHeight,
                width: this_imgWidth,
                border: false
            });
        me.getMolContainer().update(html);
    },
    onSubmit: function () {
        var me = this;
        var formOjbect = me.form.getForm().getValues();
        App.AjaxPost('/api/services/app/substance/CreateSubstance', formOjbect, function (data)
        {
            me.fireEvent("savesuccess");
        });
    }
});